package Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class dome12 {
    public static void main(String[] args) {
        //创建map集合
        Map<Integer,Student> map=new HashMap<>();
        //创建元素对象
        Student student1=new Student("张三",12);
        Student student2=new Student("王五",13);
        Student student3=new Student("李四",14);
        Student student4=new Student("牛五",15);
        //将元素添加到集合
        map.put(1,student1);
        map.put(2,student2);
        map.put(3,student3);
        map.put(4,student4);
        //利用键打印集合
        System.out.println(map.get(1));
        System.out.println(map.get(2));
        System.out.println(map.get(3));
        System.out.println(map.get(4));

        System.out.println("----------");

        //遍历集合
        for (int i = 0; i < map.size(); i++) {
            System.out.println(map.get(i+1));
        }

        System.out.println("----------");

        //增强for循环遍历集合
        for (Student value : map.values()) {
            System.out.println(value);
        }

        System.out.println("----------");

        //使用迭代器遍历集合
        Iterator iterator=map.values().iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
